package com.fau.fm.service.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.validation.ConstraintViolation;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.fau.fm.entity.MedicalKeyValue;
import com.fau.fm.entity.MedicalLeaveDeduct;
import com.fau.fm.service.IStaffSalaryManagerService;
import com.fau.fm.util.Page;
import com.fau.fm.util.ParamUtil;
import com.fau.fm.util.SystemConfig;
import com.fau.fm.util.ValidatorUtil;

/**
 * @desc 薪资管理实现类
 * @author liy
 * @datetime 2014年11月18日,上午10:07:58
 */
@Service
@Transactional(readOnly = true)
public class StaffSalaryManagerService extends BaseService implements IStaffSalaryManagerService {

	@Override
	public Page findLeaveDeductByPage(Map<String, Object> map, String hospitalId) {
		StringBuilder statusSql = new StringBuilder("from MedicalLeaveDeduct m ");
		StringBuilder countSql = new StringBuilder("select count(*) from MedicalLeaveDeduct m ");
		List<Object> objectList = new ArrayList<Object>();
		
		//排序
		String[] sortField = (String[]) map.get("sortField");
		String[] sortOrder = (String[]) map.get("sortOrder");
		if(StringUtils.isNotBlank(sortField[0]) && StringUtils.isNotBlank(sortOrder[0])) {
			statusSql.append("order by m." + sortField[0] + " " + sortOrder[0]); 
		} else {
			statusSql.append(" order by m.createTime desc ");
		}
		
		Page page = this.iBaseDao.getPage(ParamUtil.getInteger(map,"pageIndex",1),ParamUtil.getInteger(map, "pageSize", 20), map, countSql.toString(), statusSql.toString(), objectList);
		List<MedicalLeaveDeduct> list = new ArrayList<MedicalLeaveDeduct>();
		for(Object obj : page.getResult()) {
			MedicalLeaveDeduct deduct = (MedicalLeaveDeduct) obj;
			MedicalKeyValue keyValue = this.iBaseDao.getValueByKey(hospitalId, SystemConfig.HM_SYSTEM_TYPE, deduct.getLeaveType());
			deduct.setLeaveTypeName(keyValue.getKeyValue());
			list.add(deduct);
		}
		return page;
	}

	@Override
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public boolean insertLeaveDeduct(String type,String deduct) {
		MedicalLeaveDeduct leaveDeduct = (MedicalLeaveDeduct) this.iBaseDao.getObjectByHql("from MedicalLeaveDeduct a where a.leaveType=? ", type);
		if(null == leaveDeduct){
			leaveDeduct = new MedicalLeaveDeduct();
		}
		leaveDeduct.setLeaveType(type);
		leaveDeduct.setDeduct(BigDecimal.valueOf(Double.valueOf(deduct)));
		Set<ConstraintViolation<Object>> errorInfo = ValidatorUtil.validateObj(leaveDeduct);
		if(errorInfo.size() > 0) {
			return false;
		} else {
			return this.iBaseDao.saveObject(leaveDeduct);
		}
	}

	@Override
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public void deleteLeaveDeduct(String ids) {
		String[] temp = ids.split("-");
		for (String s : temp) {
			this.iBaseDao.updateHql("delete from MedicalLeaveDeduct a where a.id=? ", s);
		}
	}

	@Override
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public boolean updateLeaveDeduct(String id, String type, String deduct) {
		MedicalLeaveDeduct leaveDeduct = (MedicalLeaveDeduct) this.iBaseDao.getObject(MedicalLeaveDeduct.class, id);
		leaveDeduct.setDeduct(BigDecimal.valueOf(Double.valueOf(deduct)));
		leaveDeduct.setLeaveType(type);
		leaveDeduct.setUpdateTime(new Date());
		Set<ConstraintViolation<Object>> errorInfo = ValidatorUtil.validateObj(leaveDeduct);
		if(errorInfo.size() > 0) {
			return false;
		} else {
			return this.iBaseDao.saveObject(leaveDeduct);
		}
	}

	@Override
	public Boolean findLeaveTypeById(String tid) {
		MedicalLeaveDeduct leaveDeduct = (MedicalLeaveDeduct) this.iBaseDao.getObjectByHql("from MedicalLeaveDeduct a where a.leaveType=? ", tid);
		if(null == leaveDeduct){
			return false;
		}else{
			return true;
		}
	}

}
